<template>
  <div>
    <!--搜索查询-->
    <el-row>
      <!-- <el-col :span="">&nbsp;</el-col> -->
      <el-col :span="4"
        ><el-input
          v-model="searchForm.roleName"
          size="mini"
          clearable
          placeholder="请输入角色名称"
        ></el-input
      ></el-col>
      <el-col :span="2"
        ><el-button
          type="primary"
          size="mini"
          @click="search()"
          icon="el-icon-search"
          v-has="'coupon:search'"
          >搜索</el-button
        ></el-col
      >
      <el-col :span="2"
        ><el-button
          type="primary"
          size="mini"
          @click="handlerAdd()"
          icon="el-icon-circle-plus"
          v-has="'coupon:insert'"
          >添加</el-button
        ></el-col
      >
      <el-col :span="2"
        ><el-button
          type="primary"
          size="mini"
          v-has="'coupon:flush'"
          @click="flush()"
          icon="el-icon-refresh"
          >刷新</el-button
        ></el-col
      >
    </el-row>

    <el-row>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column
          prop="type"
          label="优惠券类型"
          :formatter="zhFormat"
          align="center"
          width="150"
        >
        </el-table-column>
        <el-table-column
          prop="name"
          label="消费券名称"
          align="center"
          width="200"
        >
        </el-table-column>
        <el-table-column
          prop="platform"
          :formatter="PRFormat"
          width="150"
          label="使用平台"
        >
        </el-table-column>
        <el-table-column
          prop="count"
          label="消费券数量"
          align="center"
          width="200"
        >
        </el-table-column>
        <el-table-column prop="amount" label="金额" align="center" width="200">
        </el-table-column>
        <el-table-column
          prop="perLimit"
          label="每人限领张数"
          align="center"
          width="200"
        >
        </el-table-column>
        <el-table-column
          prop="minPoint"
          :formatter="ABFormat"
          width="150"
          label="使用平台"
        >
        </el-table-column>
        <el-table-column
          prop="startTime"
          label="开始时间"
          align="center"
          width="200"
        >
        </el-table-column>
        <el-table-column
          prop="endTime"
          label="结束时间"
          align="center"
          width="200"
        >
        </el-table-column>
        <el-table-column
          prop="useType"
          :formatter="ACFormat"
          width="150"
          label="使用类型"
        >
        </el-table-column>
        <el-table-column prop="note" width="150" label="备注">
        </el-table-column>
        <el-table-column prop="publishCount" width="150" label="发行数量">
        </el-table-column>
        <el-table-column prop="useCount" width="150" label="已使用数量">
        </el-table-column>
        <el-table-column prop="receiveCount" width="150" label="领取数量">
        </el-table-column>
        <el-table-column prop="enableTime" width="150" label="可以领取的日期">
        </el-table-column>
        <el-table-column prop="code" width="150" label="优惠码">
        </el-table-column>
        <el-table-column
          prop="memberLevel"
          :formatter="ADFormat"
          width="150"
          label="可领取的会员类型"
        >
        </el-table-column>
        <el-table-column prop="shopName" width="150" label="所属门店名称">
        </el-table-column>
        <!-- <el-table-column
          prop="delFlag"
          :formatter="delFormat"
          width="150"
          label="删除状态"
        > 
        </el-table-column>-->
        <el-table-column label="操作" fixed="right" align="center" width="150">
          <template slot-scope="scope">
            <el-button
              size="mini"
              v-has="'coupon:update'"
              @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              v-has="'coupon:delete'"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-row>

    <el-row>
      <!--角色添加/更新对话框-->
      <el-dialog :title="title" :visible.sync="dialogFormVisible">
        <el-form :model="form" :rules="rules" ref="form">
          <el-form-item
            label="优惠卷类型"
            prop="type"
            :label-width="formLabelWidth"
          >
            <el-select
              v-model="form.type"
              placeholder="请选优惠券类型"
              size="mini"
            >
              <el-option label="全场赠券" value="0"></el-option>
              <el-option label="会员赠券" value="1"></el-option>
              <el-option label="消费赠券" value="2"></el-option>
              <el-option label="注册赠券" value="3"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item
            label="消费券名称"
            prop="name"
            :label-width="formLabelWidth"
          >
            <el-input v-model="form.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item
            label="使用平台"
            prop="platform"
            :label-width="formLabelWidth"
          >
            <el-select
              v-model="form.platform"
              placeholder="请选使用平台"
              size="mini"
            >
              <el-option label="全部" value="0"></el-option>
              <el-option label="移动" value="1"></el-option>
              <el-option label="PC" value="2"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item
            label="消费券数量"
            prop="count"
            :label-width="formLabelWidth"
          >
            <el-input v-model="form.count" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item
            label="消费券金额"
            prop="amount"
            :label-width="formLabelWidth"
          >
            <el-input v-model="form.amount" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item
            label="每人限领张数"
            prop="perLimit"
            :label-width="formLabelWidth"
          >
            <el-input v-model="form.perLimit" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item
            label="使用门槛"
            prop="minPoint"
            :label-width="formLabelWidth"
          >
            <el-select
              v-model="form.minPoint"
              placeholder="请选使用使用门槛"
              size="mini"
            >
              <el-option label="全部" value="0"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item
            label="使用类型"
            prop="useType"
            :label-width="formLabelWidth"
          >
            <el-select
              v-model="form.useType"
              placeholder="请选使用类型"
              size="mini"
            >
              <el-option label="全场通用" value="0"></el-option>
              <el-option label="指定分类" value="1"></el-option>
              <el-option label="指定车辆" value="2"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="备注" prop="note" :label-width="formLabelWidth">
            <el-input v-model="form.note" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item
            label="发行数量"
            prop="publishCount"
            :label-width="formLabelWidth"
          >
            <el-input v-model="form.publishCount" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item
            label="优惠码"
            prop="code"
            :label-width="formLabelWidth"
          >
            <el-input v-model="form.code" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item
            label="可领取的会员类型"
            prop="memberLevel"
            :label-width="formLabelWidth"
          >
            <el-select
              v-model="form.memberLevel"
              placeholder="请选可领取的会员类型"
              size="mini"
            >
              <el-option label="无限时" value="0"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item
            label="选择所属门店"
            prop="shopId"
            :label-width="formLabelWidth"
          >
            <el-select v-model="form.shopId" placeholder="请选择">
              <el-option
                v-for="item in optionsA"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="submitForm('form')"
            >确 定</el-button
          >
        </div>
      </el-dialog>
    </el-row>

    <el-row>
      <!--分页插件-->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[2, 3, 5, 10, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-row>
  </div>
</template>

<script>
export default {
  data() {
    return {
      data: [], //权限树数组对象
      defaultProps: {
        //父对象传值
        children: "children",
        label: "label",
      },
      optionsA: [],
      nodeChecked: [], //勾选项目数组对象
      id: 0, //因为角色ID在不同的js方法种使用，定义为全局的
      tableData: [],
      title: "角色添加", //弹出对话框标题
      dialogFormVisible: false, //是否显示添加更新对话框  默认是不显示
      form: { roleName: "", remark: "", status: "0" }, //form表单对象
      currentPage: 1, //页码 第几页
      pageSize: 5, //每页显示数量
      total: 100, //总条数
      searchForm: { roleName: "" }, //搜索form表单
      dialogFormVisible: false, //是否显示对话框  默认是不显示
      form: {}, //form表单对象
      menuTreeVisible: false, //权限树是否显示
      formLabelWidth: "120px",
      rules: {
        roleName: [
          { required: true, message: "请输入角色名称", trigger: "blur" },
        ],
        remark: [
          { required: true, message: "请输入角色描述", trigger: "blur" },
          {
            min: 3,
            max: 50,
            message: "长度在 2 到 50 个字符",
            trigger: "blur",
          },
        ],
        status: [
          { required: true, message: "请选择角色状态", trigger: "change" },
        ],
      },
    };
  },
  created() {
    //初始化方法 类似于jquery  $({})
    this.loadDataA();
    this.loadData();
  },
  //所有函数
  methods: {
    //加载数据
    loadData() {
      var _this = this;
      //复制 $http官网代码
      // var token  = localStorage.getItem("token");
      _this.$http
        .post("/coupon/selectAllAndShop", {
          pageNo: this.currentPage,
          pageSize: this.pageSize,
          data: {
            name: this.searchForm.roleName,
          },
        })
        .then(function (response) {
          if (response.data.code == 200) {
            _this.tableData = response.data.data.records;
            _this.total = response.data.data.total;
          }
        })
        .catch(function (error) {
          console.log(error);
          _this.$message.error("你错了。。。");
        });
    },
    loadDataA() {
      //复制 $http官网代码
      // var token  = localStorage.getItem("token");
      var _this = this;
      //加载添加的部门下拉数据
      _this.$http
        .post("/shop/selectAll",{
          pageNo: 1,
          pageSize: 100,
          data: {},
        }) //指定请求的url及参数  get
        .then(function (response) {
          //正确返回对象的处理
          //JSON.stringify   把json转换为json字符串，方便查看
          console.log(JSON.stringify(response));
          //判断返回值是否正确
          if (response.data.code == 200) {
            _this.optionsA = response.data.data.records;
          }
        });
    },
    //处理更新
    handleEdit(index, row) {
      var _this = this;
      //row  当前行数据 如果是第一行  row = {deptId:1,deptName:'管理部'把一行数据赋值给form赋值,description:'店长的事',status:1}
      console.log(index, row);
      //显示对话框
      _this.dialogFormVisible = true;
      //指定对话框标题
      _this.title = "角色更新";
      //把一行数据赋值给form赋值
      _this.form = row;
      //把数字1变为字符串1
      _this.form.platform = row.platform + "";
      _this.form.type = row.type + "";
      _this.form.minPoint = row.minPoint + "";
      _this.form.useType = row.useType + "";
      _this.form.memberLevel = row.memberLevel+ "";
    },
    //处理添加
    handlerAdd() {
      //显示对话框
      this.dialogFormVisible = true;
      //指定对话框标题
      this.title = "角色添加";
      //让form表单重新赋空值
      this.form = {};
    },
    //处理删除
    handleDelete(index, row) {
      //确认框提示
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //让_this等于当前vue对象
          var _this = this;
          //alert("删除的roleId为"+row.roleId+"删除的roleName"+row.roleName);
          // alert(111);
          //赋值 $http官网代码  this.form =form:{deptName:'',description:'',status:1}
          _this.$http
            //.get("/role/updateDelFlag?roleId=" + row.roleId) //指定请求的url及参数  get
            .delete("/coupon/delete?id=" + row.id)
            .then(function (response) {
              //正确返回对象的处理
              // handle success
              console.log(response);
              //JSON.stringify   把json转换为json字符串，方便查看
              console.log(JSON.stringify(response));
              //直接使用this 会指 $http对象  错误
              // this.tableData
              //判断返回值是否正确
              if (response.data.code == 200) {
                //提示添加成功
                _this.$message({
                  message: "恭喜你，这是一条成功消息",
                  type: "success",
                });
                //重新加载数据
                _this.loadData();
              } else {
                //错误提示信息
                _this.$message.error("错了哦，这是一条错误消息");
              }
            })
            .catch(function (error) {
              //错误的处理
              // handle error
              console.log(error);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    zhFormat(row, column, cellValue, index) {
      var returnValue = "";
      if (cellValue == 0) {
        returnValue = "全场赠券";
      } else if (cellValue == 1) {
        returnValue = "会员赠券";
      } else if (cellValue == 2) {
        returnValue = "消费赠券";
      } else if (cellValue == 3) {
        returnValue = "注册赠券";
      }
      return returnValue;
    },
    PRFormat(row, column, cellValue, index) {
      var returnValue = "";
      if (cellValue == 0) {
        returnValue = "全部";
      } else if (cellValue == 1) {
        returnValue = "移动";
      } else if (cellValue == 2) {
        returnValue = "PC";
      }
      return returnValue;
    },
    ABFormat(row, column, cellValue, index) {
      var returnValue = "";
      if (cellValue == 0) {
        returnValue = "无门槛";
      }
      return returnValue;
    },
    ACFormat(row, column, cellValue, index) {
      var returnValue = "";
      if (cellValue == 0) {
        returnValue = "全场通用";
      } else if (cellValue == 1) {
        returnValue = "指定分类";
      } else if (cellValue == 2) {
        returnValue = "指定车辆";
      }
      return returnValue;
    },
    ADFormat(row, column, cellValue, index) {
      var returnValue = "";
      if (cellValue == 0) {
        returnValue = "无限时";
      }
      return returnValue;
    },
    delFormat(row, column, cellValue, index) {
      //row当前行数据
      //column 列名称
      //cellValue 列的值
      //index 下标
      //alert(cellValue);
      return cellValue == 0 ? "存在" : "消失了";
    },
    //处理每页数量发生变化
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      //让每页数量发生变化
      this.pageSize = val;
      //重载数据
      this.loadData();
    },
    //处理当前页变化
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      //让每页数量发生变化
      this.currentPage = val;
      //重载数据
      this.loadData();
    },
    //查询
    search() {
      this.loadData();
    },
    //刷新
    flush() {
      location.reload();
    },
    //取消按钮
    dialogFormVisiblExit() {
      this.form = "";
      this.dialogFormVisibleAdd = false;
    },

    //提交添加更新表单
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var url = "";
          if (this.title == "角色添加") {
            url = "/coupon/insert";
          } else {
            url = "/coupon/update";
          }
          //让_this等于当前vue对象
          var _this = this;
          // alert(111);
          //赋值 axios官网代码  this.form =form:{deptName:'',description:'',status:1}
          this.$http
            .put(url, this.form)
            .then(function (response) {
              //正确返回对象的处理
              // handle success
              console.log(response);
              //JSON.stringify   把json转换为json字符串，方便查看
              console.log(JSON.stringify(response));
              //直接使用this 会指 axios对象  错误
              // this.tableData
              //判断返回值是否正确
              if (response.data.code == 200) {
                //关闭弹框
                _this.dialogFormVisible = false;
                //提示添加成功
                _this.$message({
                  message: "恭喜你，这是一条成功消息",
                  type: "success",
                });
                //重新加载数据
                _this.loadData();
              } else {
                //错误提示信息
                _this.$message.error("错了哦，这是一条错误消息");
              }
            })
            .catch(function (error) {
              //错误的处理
              // handle error
              console.log(error);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>
<style>
</style>
